package com.pengweiwei.androidzygote.goods.detail;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.appbar.AppBarLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentStatePagerItemAdapter;
import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.activity.LoadableActivity;
import com.putong35.common.httprequest.IHttpRequest;
import com.putong35.common.httprequest.UrlParams;
import com.putong35.common.httprequest.app.MyHttpRequest;
import com.putong35.common.httprequest.responsecallbacks.SingleBeanResponseListener;
import com.putong35.common.utils.ActivityUtils;
import com.putong35.common.utils.ui.HeaderContentLayoutWraper;
import com.putong35.common.web.ui.HtmlContentWebFragment;
import com.pengweiwei.androidzygote.R;
import com.pengweiwei.androidzygote.databinding.ActivityGoodsDetailBinding;
import com.pengweiwei.androidzygote.databinding.BodyGoodsDetailBinding;
import com.pengweiwei.androidzygote.databinding.HeaderGoodsDetailBinding;
import com.pengweiwei.androidzygote.databinding.HeaderGoodsDetailTabsBinding;
import com.pengweiwei.androidzygote.goods.CommentListFragment;
import com.pengweiwei.androidzygote.goods.GoodsBean;
import com.pengweiwei.androidzygote.main.UrlCenter;

import static com.pengweiwei.androidzygote.goods.CommentListFragment.KEY_GOODS_BEAN;

/**
 * Simple to Introduction
 *
 * @Author: ${USER}
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: ${DATE} ${TIME}
 * @Version: 1.0.0
 */

public class GoodsDetailActivity extends LoadableActivity {

    public static final String KEY_GOODS_ID = "key_goods_id";

    private int originTabLayoutY;
    int moveEndY;


    ActivityGoodsDetailBinding mainBinding;
    HeaderContentLayoutWraper headerContentSI;
    HeaderGoodsDetailBinding headerGoodsDetailBinding;
    HeaderGoodsDetailTabsBinding headerGoodsDetailTabsBinding;
    BodyGoodsDetailBinding bodyGoodsDetailBinding;
    private String goodsId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
        statusTransucentAndLayoutFullscreen();
        goodsId = ActivityUtils.getBundle(getIntent()).getString(KEY_GOODS_ID);


        headerGoodsDetailTabsBinding.tabLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                int[] location = new int[2];
                headerGoodsDetailTabsBinding.tabLayout.getLocationOnScreen(location);
                originTabLayoutY = location[1];
                moveEndY = mainBinding.toolbar11.getMeasuredHeight();
                Log.e("pww", "ttt = moveEndY" + moveEndY);
            }
        }, 300);

        mainBinding.toolbar11.setBackgroundResource(R.color.def_theme_color_primary);
        mainBinding.toolbar11.getBackground().setAlpha(255);

        headerContentSI.getAppBarLayout().addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int[] location = new int[2];
                headerGoodsDetailTabsBinding.tabLayout.getLocationOnScreen(location);

                int alpha = 255 - (int) (255.0f * (location[1] - moveEndY) / (originTabLayoutY - moveEndY));
                if (alpha < 0) {
                    alpha = 0;
                } else if (alpha > 255) {
                    alpha = 255;
                }
                Log.e("pww", "alpha = " + alpha);
                mainBinding.toolbar11.getBackground().setAlpha(alpha);
            }
        });
        startLoading();

    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mainBinding = ActivityGoodsDetailBinding.inflate(inflater);
        headerGoodsDetailBinding = HeaderGoodsDetailBinding.inflate(inflater);
        headerGoodsDetailTabsBinding = HeaderGoodsDetailTabsBinding.inflate(inflater);
        bodyGoodsDetailBinding = BodyGoodsDetailBinding.inflate(inflater);

        headerContentSI = new HeaderContentLayoutWraper(this, mainBinding.lytContent);
        headerContentSI.getRefreshLayout().setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
//                reload();
            }
        });
        headerContentSI.addHeaderView(headerGoodsDetailBinding.getRoot());
        headerContentSI.addHeaderView(headerGoodsDetailTabsBinding.getRoot(), 0);
        headerContentSI.addContentView(bodyGoodsDetailBinding.getRoot());

        return mainBinding.getRoot();
    }



    @Override
    public void onStartLoading() {
        UrlParams urlParams = new UrlParams();
        urlParams.put("id", goodsId);
        IHttpRequest request = new MyHttpRequest(this);
        request.get(UrlCenter.GOODS_DETAIL, urlParams, new SingleBeanResponseListener<GoodsBean>(GoodsBean.class) {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                setState(CompState.DATA);
            }

            @Override
            public void onSuccess(GoodsBean bean) {
                mainBinding.setViewModel(bean);
                mainBinding.executePendingBindings();
                mainBinding.invalidateAll();
                mainBinding.notifyChange();
                headerGoodsDetailBinding.setViewModel(bean);
                setState(CompState.DATA);

                FragmentPagerItems.Creator creator = FragmentPagerItems.with(GoodsDetailActivity.this);
                Bundle detailBundle = new Bundle();
                String html = "<html><head><meta charset=\"utf-8\" /><meta name=\"viewport\" content=\"width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no\"/><title>爬虫科技有限公司</title></head><body><p><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/c621161e-5a17-44da-b3db-81f87db6a56b.png?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/fe4ad3aa-d31c-4499-b889-24614f275c38.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/2a699f09-60ae-41fe-ab77-d91da6109d9a.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/7def5472-e3ec-4d8f-8ae1-90c304992a89.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/c4a8b4a3-faa1-4007-ab79-968c55d165f5.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/16a96815-ff7c-4ac6-b8a4-933fcd4551f7.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/a0cd25da-afb5-4625-86c1-f155b7699b72.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/8d75e87b-59af-4e4a-ad57-26feafbb282e.png?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/aa9fd8ee-0284-4a64-a984-3e7d61a5a3eb.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/a07692c2-c46a-4125-9c9b-83c4b3dd3289.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/553b9958-030d-4beb-a671-591d032f3b7f.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/8c39ba70-bfb3-4968-b563-6bc3d5f9c278.jpg?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/6880a1e3-2b58-4b01-b6af-0ef96d882947.png?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/63a9849f-170d-4b68-94f7-aa0fb0580bb6.gif?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/d99f2a33-610d-4cbe-b335-f771ca0d0a6b.gif?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/76abbf76-9d50-402e-a958-8e10ced1bd5f.png?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/356afcc9-c2a8-416b-82ce-e82116fd3524.png?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/234b1594-a33d-4300-afa6-027778a082d8.png?imageView2/2/w/0/h/0/q/80\" /><img style=\"width:100%;\" src=\"http://or84r4lrb.bkt.clouddn.com/7264dc20-8463-4985-b80c-bd90f1da4a07.jpg?imageView2/2/w/0/h/0/q/80\" /></p><p><br/></p></body></html>";
                detailBundle.putString("htmlContent", html);
                creator.add("详情", HtmlContentWebFragment.class, detailBundle);
                Bundle commentBundle = new Bundle();
                commentBundle.putSerializable(KEY_GOODS_BEAN, bean);
                creator.add("评论", CommentListFragment.class, commentBundle);

                FragmentStatePagerItemAdapter adapter = new FragmentStatePagerItemAdapter(
                        getSupportFragmentManager(), creator.create());
                bodyGoodsDetailBinding.viewPager.setAdapter(adapter);
                headerGoodsDetailTabsBinding.tabLayout.setViewPager(bodyGoodsDetailBinding.viewPager);
                setCommentCountToTab(bean.getComment_count());
            }
        });

    }

    public void setCommentCountToTab(int count) {
        String commentTitle = "评价（" + count + "）";
        View tabView = headerGoodsDetailTabsBinding.tabLayout.getTabAt(1);
        if (tabView instanceof TextView) {
            TextView textView = (TextView) tabView;
            textView.setText(commentTitle);
        }

    }

}
